/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Advertentie;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import persistence.SessionManager;

/**
 *
 * @author Niels Leguit <niels.leguit at hva.nl>
 */
public class AdvertentieGetImageController extends HttpServlet {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("-----------------------> AdvertentieGetImageController - doGet - begin");

        doPost(request, response);

        System.out.println("-----------------------> AdvertentieGetImageController - doGet - einde");

    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("-----------------------> AdvertentieGetImageController - doPost begin");

        Session session = null;
        session = SessionManager.openSession();

        // haal uit url of advertentie nieuw is of een reeds bestaande
        int advId;
        try {
            advId = Integer.parseInt(request.getParameter("advId"));
            System.out.println("advId: " + advId);
        } catch (Exception e) {
            advId = 0;
        }

        //advertentie ophalen
        Advertentie adv = (Advertentie) session.load(Advertentie.class, new Integer(advId));

        Blob imageToReturn = Hibernate.createBlob(adv.getAdvImage());

        // Set content type
        response.setContentType("image/jpeg");
        InputStream in = null;
        OutputStream out = null;
        try {
            response.setContentLength((int) imageToReturn.length());
            // Open the file and output streams
            in = imageToReturn.getBinaryStream();
            out = response.getOutputStream();
            // Copy the contents of the file to the output stream
            byte[] buf = new byte[1024];
            int count = 0;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }
            long end = System.currentTimeMillis();
        } catch (SQLException e) {
            System.out.println("--> e");
        } finally {
        }

        System.out.println("-----------------------> AdvertentieGetImageController - doPost - einde");

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
